package org.springframework.prospring.ticket.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.prospring.ticket.dao.UserDAO;
import org.springframework.prospring.ticket.domain.user.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.prospring.ticket.exception.BusinessException;


/**
 * This class interacts with Spring and Hibernate to save and
 * retrieve User objects.
 *
 * @author Alexis Quiros
 */
public class UserDAOHibernate extends HibernateDaoSupport implements UserDAO {
    private static Log log = LogFactory.getLog(UserDAOHibernate.class);

    public UserDAOHibernate() {
        super();
        if (log.isDebugEnabled()) {
            log.debug("in UserDAOHibernate Constructor");
        }
    }

    public List getUsers() {
        if (log.isDebugEnabled()) {
            log.debug("getting Users");
        }

        List users = getHibernateTemplate().find("from User");
        return users;
    }

    public User findUser(Long id) {
        if (log.isDebugEnabled()) {
            log.debug("getting User id: " + id);
        }
        return (User) getHibernateTemplate().get(User.class, id);
    }

    public void saveUser(User user) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Saving User: " + user);
        }

        getHibernateTemplate().saveOrUpdate(user);

        if (user.getFirstName().equals(user.getLastName())) {
            String message = "The first name is equals to last name, firstName="+user.getFirstName();
            log.info(message);
            throw new BusinessException(message);
        }

        if (user.getFirstName().equals("Alexis")) {
            String message = "The first name is equals ="+user.getFirstName();
            log.info(message);
            throw new Exception(message);
        }


        if (log.isDebugEnabled()) {
            log.debug("userId set to: " + user.getId());
        }
    }

    public void removeUser(Long id) {
        if (log.isDebugEnabled()) {
            log.debug("removing User id: " + id);
        }

        Object user = getHibernateTemplate().load(User.class, id);
        getHibernateTemplate().delete(user);        
    }

    public User checkUser(String nickname, String password){
        String []paramsNames = new String[] {"nickname", "password"};
        String []paramsValues = new String[] {nickname, password};
        List<User> userList = getHibernateTemplate().findByNamedQueryAndNamedParam("findUserByNicknameAndPassword", paramsNames, paramsValues);

        System.out.println("userList = " + userList);

        User user = null;
        if (userList != null && userList.size() > 0){
            if (userList.size() == 1){
                user = userList.get(0);
            } else {
                // error  no puede haber mas de un usuario con la mismos datos
                System.out.println("ERROR se encontraron mas de un usuario");
            }

        }

        return user;
    }

}
